﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 广告数据库操作类
    /// </summary>
    public class DaoFeedback : DAO
    {
        /// <summary>
        /// 添加一条反馈
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Add(MFeedback m)
        {
            String sql = "insert into feedback(contact,content)values(@contact,@content)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@contact", DbType.String, m.contact);
            db.AddInParameter(cmd, "@content", DbType.String, m.content);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MFeedback GetEntity(int id)
        {
            String sql = "select contact,content,CONVERT(varchar(20), [date], 20) [date] from feedback where id = " + id;
            object obj = BindEntity(sql, new MAdvert());
            return obj == null ? null : (MFeedback)obj;
        }

        /// <summary>
        /// 删除一条反馈
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public static bool Delete(int id)
        {
            String sql = "delete feedback where id = " + id;
            return ExecuteNonQuery(sql) > 0;
        }


        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetList(int pageIndex, int pageSize)
        {
            String fieldList = "id,contact,content,CONVERT(varchar(20), [date], 20) [date]";
            String orderBy = "id desc";
            String where = "";
            String tableName = "feedback";
            return GetPageList(fieldList, orderBy, pageIndex, pageSize, where, tableName);
        }
    }
}
